package com.example.readandwriteseparation.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 动态数据源
 *
 * @author qingyan 2021-08-15
 */
@Configuration
public class DataSourceConfig {

    /**
     * 将所有数据源注册成bean的方案有局限性
     * 适用于读写分离等少数问题
     * 在数据源太多或者有数据源不能连接的时候就会存在问题
     */
    @Bean(name = "master")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DruidDataSource master() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "slaver")
    @ConfigurationProperties(prefix = "spring.datasource.slaver")
    public DruidDataSource slaver() {
        return DruidDataSourceBuilder.create().build();
    }
}
